HOMEWORK 1, Due Tuesday, Jan 16th, before class

 

  1. Write a C program to replace a string of blanks with a single blank.
  2. Write a function expand(s, t) which converts characters like newline and tab into visible escape sequences line \n and \t as it copies the string s to t.Use a switch.  
  3. Write a C program that converts a character from lowercase to uppercase.
  4. Given below is portion of a program that takes as input the letter grade of a student and prints it. Is this  porition of the program correct? If not, pick out the mistakes and write the correct portion of the program. Assume that GRADE is of type char.

 

        getchar(GRADE) ;

 

        if (GRADE = 'A')

          printf("A Grade") ;    

        else if (GRADE = 'B')

              printf("B Grade");

             else if (GRADE = 'C')

                  printf("C Grade");

 

 

  1. What is the output generated by each of the following programs? Do you observe anything unusual?

 

A)

main(){

  int i, sum;

  i = 0;

  sum = i;

  printf("%d ", sum) ;

  while (i < 15) {

     i += 5;  

     sum += i ;

     printf("%d ",sum) ;

 

  }

  printf("\n %d",sum) ;

}

                      

 

B)              main() {

       int i, x = 0;

      

       for (i=1; i <= 10; i++)

         if (floor(log2(i)) == log2(i))  

           x++;

       printf(" %d ",x) ;

     }

 

C)                 

 

           main(){

              int i, x = 0;

              

               for (i=0; i<5; i++){  

                   switch(i){

                        case 0: {x++; break ;}

                        case 1: {x++; break ;}

                        case 2: {x++; break ;}

                        case 3: {x += 2; }

                        case 4: {x += 3; }

}

printf("x = %d", x) ;

x = 0 ;

              }

}

 

 

D)              main(){

int i = 0;

 

printf("%d ", i++) ;

printf("%d", --i) ;

}

 

 

 

 

  1. Describe each of the following declarations

 

a.     char **a ;

b.     char (*a)[] ;

c.     int *f() ;

d.     int *(*f)() ;

e.     int (*f())[] ;

 

   7. What will be the output of the following program?

 

main(int argc, char * argv[]){

int i ;

          for (i = 0; i< argc; i++){

printf("%c ",argv[i][i]) ;

          }

     }   

 

 

8. In the following program, which of the following expressions evaluate to the contents of p[1]?

 

          main(){

              char p[] = {'a', 'b', 'c'};

              char *q;

              q = p ;

          }

 

a)     *++q

b)     *(++q)

c)     *(q+1)

d)     *(p+1)

e)     1+p